<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: Metas.class.php 142 2012-04-26 10:02:56Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class Metas {
	
	/**
	 * Metas 模型句柄
	 *
	 * @access public
	 * @var string
	 */
	public $mModel = NULL;
	
	/**
	 * Relationships 模型句柄
	 *
	 * @access public
	 * @var string
	 */
	public $rModel = NULL;
	
	/**
	 * 错误提示
	 *
	 * @access public
	 * @var string
	 */
	public $error = '';
	
	/**
	 * Metas 类型
	 *
	 * @access private
	 * @var string
	 */
	private $type = 'category';
	
	/**
	 * Metas 别名
	 *
	 * @access private
	 * @var string
	 */
	private $slug = '';
	
	/**
	 * Metas 名称
	 *
	 * @access private
	 * @var string
	 */
	private $name = '';
	
	/**
	 * 文章 id
	 *
	 * @access private
	 * @var int
	 */
	private $postid = 0;
	
	/**
	 * Metas id
	 *
	 * @access private
	 * @var int
	 */
	private $metaid = 0;
	
	/**
	 * Metas neq id
	 *
	 * @access private
	 * @var int
	 */
	private $unique = 0;
	
	/**
	 * 构造函数
	 *
	 * @access public
	 * @var void
	 */
	public function __construct() {
		$this->mModel = D ( 'Metas' );
		$this->rModel = D ( 'Relationships' );
	}
	
	/**
	 * 设置 Metas 类型
	 *
	 * @access public
	 * @param string $type Metas 类型
	 * @var void
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * 获取 Metas 类型
	 *
	 * @access public
	 * @param string $type Metas 类型
	 * @var void
	 */
	public function getType($type) {
		return $this->type;
	}
	
	/**
	 * 设置 Metas 别名
	 *
	 * @access public
	 * @param string $slug Metas 别名
	 * @var void
	 */
	public function setSlug($slug) {
		$this->slug = $slug;
	}
	
	/**
	 * 设置 Metas 名称
	 *
	 * @access public
	 * @param string $name Metas 名称
	 * @var void
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * 设置文章 id
	 *
	 * @access public
	 * @param string $postid 文章 id
	 * @var void
	 */
	public function setPostid($postid) {
		$this->postid = intval ( $postid );
	}
	
	/**
	 * 设置Metas id
	 *
	 * @access public
	 * @param string $metaid Metas id
	 * @var void
	 */
	public function setMetaid($metaid) {
		$this->metaid = intval ( $metaid );
	}
	
	/**
	 * 获取 Meta 信息
	 *
	 * @access public
	 * @return array
	 */
	public function getMetas() {
		
		$map = array ();
		$type = $this->type;
		$slug = $this->slug;
		$name = $this->name;
		$postid = $this->postid;
		$metaid = $this->metaid;
		$unique = $this->unique;
		
		if ($postid) {
			$map ['postid'] = $postid;
		} else {
			if ($type) {
				$map ['type'] = $type;
			}
			if ($slug) {
				$map ['slug'] = $slug;
			}
			if ($name) {
				$map ['name'] = $name;
			}
			if ($metaid) {
				$map ['metaid'] = $metaid;
			}
			if ($unique) {
				$map ['metaid'] = array ('neq', $unique );
			}
			return $this->mModel->getListByMap ( $map );
		}
	}
	
	/**
	 * 添加一个 Meta
	 *
	 * @access public
	 * @return mix
	 */
	public function addMetas() {
		
		/** ThinkPHP自动验证,自动完成 */
		if (false === $this->mModel->create ()) {
			$this->error = $this->mModel->getError ();
			return false;
		}
		
		if ($this->type == 'category' || $this->type == 'tag') {
			/** 检查名称唯一性 */
			$this->name = $this->mModel->name;
			if ($this->getMetas ()) {
				$this->error = '错误：项目已存在';
				return false;
			}
			
			/** 检查别名唯一性 */
			$this->name = '';
			$this->slug = $this->mModel->slug;
			if ($this->slug && $this->getMetas ()) {
				$this->error = '错误：缩略名已经存在';
				return false;
			}
			
			/** 插入数据 */
			$result = $this->mModel->add ();
			
			if ($result !== false) {
				return $result;
			} else {
				$this->error = '错误：未知错误';
				return false;
			}
		}
	}
	
	/**
	 * 编辑一个 Meta
	 *
	 * @access public
	 * @return mix
	 */
	public function editMetas() {
		
		/** ThinkPHP自动验证,自动完成 */
		if (false === $this->mModel->create ()) {
			$this->error = $this->mModel->getError ();
			return false;
		}
		
		/** 检查名称唯一性 */
		$this->name = $this->mModel->name;
		$this->unique = $this->mModel->metaid;
		if ($this->getMetas ()) {
			$this->error = '错误：项目已存在';
			return false;
		}
		
		/** 检查别名唯一性 */
		$this->name = '';
		$this->slug = $this->mModel->slug;
		$this->unique = $this->mModel->metaid;
		if ($this->slug && $this->getMetas ()) {
			$this->error = '错误：缩略名已经存在';
			return false;
		}
		
		/** 更新数据 */
		if (false !== $this->mModel->save ()) {
			return true;
		} else {
			$this->error = '错误：未知错误';
			return false;
		}
	}
	
	/**
	 * 删除 Meta
	 *
	 * @access public
	 * @param $metaid array
	 * @return bool
	 */
	public function deleteMetas($metaid) {
		//$pk	= $this->mModel->getPk();
		if (! empty ( $metaid )) {
			
			//$condition = array( $pk => array ( 'in', $metaid ) );
			$condition = array ('metaid' => array ('in', $metaid ) );
			
			if ($this->type == 'category') {
				
				foreach ( $metaid as $var ) {
					/** 处理删除有子类的父类, 把子类全归档到删除的父类 */
					if ($this->mModel->where ( 'parent=' . $var )->count ()) {
						$parent = $this->mModel->where ( 'metaid=' . $var )->getField ( 'parent' );
						$this->mModel->where ( 'parent=' . $var )->setField ( 'parent', $parent );
					}
				}
				/** 转移文章到默认分类 */
				$this->rModel->where ( $condition )->setField ( 'metaid', 1 );
				/** 修正默认分类文章数 */
				$count = $this->rModel->where ( 'metaid=1' )->count ();
				$this->mModel->where ( 'metaid=1' )->setField ( 'count', $count );
			
			} elseif ($this->type == 'attachment') {
				//TODO 删除附件
				$this->rModel->where ( $condition )->delete ();
			} else {
				$this->rModel->where ( $condition )->delete ();
			}
			$this->mModel->where ( $condition )->delete ();
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 增加 Meta 文章数
	 *
	 * @access public
	 * @param $metaid
	 * @return void
	 */
	public function setInc($metaid, $step = 1) {
		return $this->mModel->where ( 'metaid=' . $metaid )->setInc ( 'count', $step );
	}
	
	/**
	 * 减少 Meta 文章数
	 *
	 * @access public
	 * @param $metaid
	 * @return void
	 */
	public function setDec($metaid, $step = 1) {
		return $this->mModel->where ( 'metaid=' . $metaid )->setDec ( 'count', $step );
	}
	
	/**
	 * 增加 Meta 与文章的对应关系
	 *
	 * @access public
	 * @param $metaid
	 * @param $postid
	 * @return void
	 */
	public function addRelationships($metaid, $postid) {
		$map = array ();
		$map ['metaid'] = $metaid;
		$map ['postid'] = $postid;
		if ($this->mModel->getOneByMap ( $map, $this->mModel->getPk () ))
			return false;
		$this->setInc ( $metaid );
		return $this->rModel->add ( $map );
	}
	
	/**
	 * 删除 Meta 与文章的对应关系
	 *
	 * @access public
	 * @param $metaid
	 * @param $postid
	 * @return void
	 */
	public function deleteRelationships($metaid, $postid) {
	}
	
	/**
	 * 根据内容的指定类别和状态更新相关meta的计数信息
	 *
	 * @access public
	 * @param int $metaid meta id
	 * @param string $type 类别
	 * @param string $status 状态
	 * @return void
	 */
	public function refresh($metaid, $type, $status = 'publish') {
		
		/** 这里应该用视图模型*/
		/*$sql = "select count(p.postid) as count ".
               " from " . C( 'DB_PREFIX' ) . "posts as p ".
               " left join " . C( 'DB_PREFIX' ) . "relationships as r on r.postid = p.postid ".
			   " where p.type = '$type' and p.status = '$status' and metaid = $metaid ";
		$count = $this->query($sql);*/
		
		$map = array ();
		$map ['metaid'] = $metaid;
		$map ['type'] = $type;
		$map ['status'] = $status;
		$count = D ( 'PostsView' )->where ( $map )->find ();
		
		$this->mModel->where ( 'metaid=' . $metaid )->setField ( 'count', $count ['count'] );
	}
	
	/**
	 * 删除缓存
	 *
	 * @access $public
	 * @return void
	 */
	public function cache() {
		switch ($this->type) {
			case 'category' :
				S ( 'category_array', NULL );
				S ( 'category', NULL );
				break;
			case 'tag' :
				S ( 'tag', NULL );
				break;
		}
	}
	
	/**
	 * 返回模型的错误信息
	 *
	 * @access $public
	 * @return string
	 */
	public function getError() {
		return $this->error;
	}

}